package com.v1.video.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.SettingLoginActivity;
import com.v1.video.domain.LoginInfo;
import com.v1.video.util.Logger;
import com.v1.video.view.SubscribeViewPage;

public class MainPageThemeFragment extends V1BaseFragment implements
		IRefresh {
	private final String TAG = "MainPageThemeFragment";
	private LayoutInflater mInflater;
	private View mRootView;

//	private SubscribeViewPage mViewPager;
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private RelativeLayout layoutMyTheme;//我的主题
	private RelativeLayout layoutAllTheme;//全部主题
	private List<Fragment> mFragments;
	
	private TextView txtMyTheme;//我的主题
	private View line_1;//精选下的横线
	private View line_2;
	private View line_3;
	private View line_4;
	private TextView txtAllTheme;//全部主题
	private final int STATE_GO_THEME_ALL = 0X0002;//全部主题
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		mRootView = mInflater.inflate(R.layout.fragment_main_page_theme,
				container, false);
		initView();
		initData();
		setListener();

		return mRootView;
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("主题");
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(hidden);
		if(!hidden){
//			if(mViewPager.getCurrentItem() == 0){
//				mFragments.get(0).onHiddenChanged(hidden);
//			}
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("主题");
	}

	private void initView() {
		mViewPager = (ViewPager) mRootView
				.findViewById(R.id.subscribe_viewpage);
		layoutMyTheme = (RelativeLayout) mRootView
				.findViewById(R.id.head_theme_my_back);
		layoutAllTheme = (RelativeLayout) mRootView
				.findViewById(R.id.head_theme_all_back);
		txtMyTheme = (TextView)mRootView.findViewById(R.id.head_mytheme_txt);
		line_1 = (View)mRootView.findViewById(R.id.head_bar_line1);
		line_2 = (View)mRootView.findViewById(R.id.head_bar_line3);
		line_3 = (View)mRootView.findViewById(R.id.head_bar_line5);
		line_4 = (View)mRootView.findViewById(R.id.head_bar_line6);
		txtAllTheme = (TextView)mRootView.findViewById(R.id.head_altheme_txt);
		txtMyTheme.setText("我的订阅");
		txtAllTheme.setText("发现");
		
	}

	private void initData() {
		mFragments = new ArrayList<Fragment>();
		Fragment mTabMyTheme = new FragmentThemeMyTheme(handler);
		Fragment mTabAllTheme = new FragmentThemeAllTheme();
		mFragments.add(mTabMyTheme);
		mFragments.add(mTabAllTheme);

		mAdapter = new FragmentPagerAdapter(getActivity()
				.getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mFragments.size();
			}
			
			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				// TODO Auto-generated method stub
				super.destroyItem(container, position, object);
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				// TODO Auto-generated method stub
				return super.instantiateItem(container, position);
			}

			@Override
			public Fragment getItem(int arg0) {
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
//		mViewPager.setNoScroll(true);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				int currentItem = mViewPager.getCurrentItem();
				 setTab(currentItem);
				 if(currentItem == 0){
					 mFragments.get(currentItem).onResume();
				 }else{
					 ((FragmentThemeAllTheme)mFragments.get(currentItem)).updataRefresh();
				 }
				 
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
//				if(arg0 == 1 && !LoginInfo.getInstance().isLogin()){
//					Intent intent_login = new Intent(getActivity(),
//							SettingLoginActivity.class);
//					intent_login.putExtra("loginFlag", 4);
//					startActivityForResult(intent_login, Constant.REQUESTCODE_LOGIN_CODE);
//					return;
//				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		setSelect(1);
	}
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case STATE_GO_THEME_ALL:
				Logger.i(TAG, "来精选了");
				setSelect(1);
				break;
			}
		}
		
	};

	private void setListener() {
		layoutMyTheme.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelect(0);
			}
		});
		layoutAllTheme.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelect(1);
			}
		});
	}

	private void setSelect(int index) {
		
//		if(index == 2){
//			if(!LoginInfo.getInstance().isLogin()){
//				Intent intent_login = new Intent(getActivity(),
//						SettingLoginActivity.class);
//				intent_login.putExtra("loginFlag", 4);
//				startActivityForResult(intent_login, Constant.REQUESTCODE_LOGIN_CODE);
//				return;
//			}
//		}
		setTab(index);
		mViewPager.setCurrentItem(index);		
	}

	private void setTab(int index) {
		// 切换title颜色 以及下面内容；
		Logger.i(TAG, "当前viewpage的索引="+index);
		switch(index){
		case 0://我的主题
			txtMyTheme.setTextColor(getResources().getColor(R.color.color_text_selected));
			line_1.setVisibility(View.VISIBLE);
			line_2.setVisibility(View.GONE);
			line_3.setVisibility(View.GONE);
			line_4.setVisibility(View.VISIBLE);
			txtAllTheme.setTextColor(getResources().getColor(R.color.color_textColor_black_44));
			clickStatistics(Constant.THEME_MYTHEME_ID);
//			mFragments.get(0).onResume();
			break;
		case 1://全部主题
			txtMyTheme.setTextColor(getResources().getColor(R.color.color_textColor_black_44));
			line_1.setVisibility(View.GONE);
			line_2.setVisibility(View.VISIBLE);
			line_3.setVisibility(View.VISIBLE);
			line_4.setVisibility(View.GONE);
			txtAllTheme.setTextColor(getResources().getColor(R.color.color_text_selected));
			clickStatistics(Constant.THEME_ALLTHEME_ID);
//			mFragments.get(1).onResume();
			break;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Logger.i(TAG, "onActivityResult执行了22222222222222222");
	}
}
